package com.example.gc.myapplication;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    LayoutInflater mInflater;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.activity_main,null);
        setContentView(view);
    }
    public void doClick(View v){
        switch (v.getId()){
            case R.id.network:
                if (isNetWorkConnected(this)){
                    Toast.makeText(this,"网络连接成功",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(this,"网络连接失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.wifi:

        }
    }
    public boolean isNetWorkConnected(Context context){
        if(context!=null){
            ConnectivityManager mManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo mNetWorkInfo = mManager.getActiveNetworkInfo();
            if(mNetWorkInfo!=null){
                return mNetWorkInfo.isAvailable();
            }

        }
        return false;
    }
}
